<?php

/**
 * +---------------------------------------------------------------
 * | Author: 北京永青宇创科技有限公司
 * | Date: 2020/03/12
 * | 版本: 6.0
 * +---------------------------------------------------------------
 * | CopyRight: 北京永青宇创科技有限公司 All rights reserved.
 * +---------------------------------------------------------------
 * | 版权声明：云豹不是一个自由软件，是云豹官方推出的商业源码。
 * | 严禁在未经许可的情况下拷贝、复制、传播、使用云豹的任意代码
 * | 如有违反，请立即删除，否则您将面临承担相应，法律责任的风险。
 * | 如果需要取得官方授权，请联系官方 https://www.gepardshop.com
 * +---------------------------------------------------------------
 */

namespace app\mall\controller;

class Category extends MallHome {
    public function Index($id = 0)
    {
        $map = ['status' => 1];
        $order = 'num_stock desc';

        $parem['search'] = $search = input('search');//
        $parem['category'] = $id = input('id');
        $parem['integral'] = $integral = input('integral');
        $start = input('start');
        $end = input('end');

        $filed = input('sort_filed');
        $sort = input('sort_name');

        if(!empty($filed) && !empty($sort)){
            $order = $filed.' '.$sort;
        }

        if (empty($search) && empty($id)) {
            $this->error('请求出错');
        }

        if (!empty($integral)) {
            $map['price'] = '';
            $map['price'] = ['between', explode(',', $integral)];
        }

        if (!empty($start) || !empty($end)) {
            $map['price'] = '';
            $map['price'] = ['between',[$start?$start:'0',$end?$end:'100000']];

            $parem['start'] = $start;
            $parem['end'] = $end;
            $parem['scope'] = $start.','. $end;
        }

        if(!empty($search)){
            $map['title'] = ['like', '%' . $search . '%'];
            if (!empty($id)) {
                $map['category_id'] = $id;
//                $id_id = $id;
            }

            list($list, $pagehtml) = model('mall/mallGoods')->where($map)->order($order)->pagedata(12, false, ['query' => ['id' => $id, 'order' => $order]]);

            if(!empty($list) && count($list) >0){
                $id_id = $list[0]['category_id'];
            }else{
                $id_id = $id;
            }
            $id_list = model('mall/mallCategory')->field('id,title')->where(['status'=>1 ,'pid'=>['in',model('mall/mallCategory')->where(['status'=>1,'id'=>$id_id])->value('pid')]])->select();
            $this->assign('category', !empty($id_list) ? $id_list : [] );
        }else{

            $child_ids = $this->getChilIds($id);

            $map['category_id'] = ['in',$child_ids];
            $where = ['status' => 1, 'id' => ['in',$child_ids]];

            list($list, $pagehtml) = model('mall/mallGoods')->where('category_id','in',$child_ids)->order($order)->pagedata(12, false, ['query' => ['id' => $id, 'sort' => $order]]);
            $id_list = model('mall/mallCategory')->field('id,title')->where('status',1)->where('id','in',$child_ids)->select();
            $this->assign('category', $id_list);
        }

        $this->assign('meta_title', '');
        $this->assign('goods_list', $list);
        $this->assign('pagehtml', $pagehtml);
        $this->assign('sort_name', input('sort_name') == 'desc' ? 'asc' : 'desc');
        $this->assign('parem', $parem);

        return $this->fetch();
    }

}